from rest_framework import serializers, exceptions
from .models import OAUser, UserStatusChoices, OADepartment


class LoginSerializer(serializers.Serializer):
    email = serializers.EmailField(required=True, error_messages={"required": "请输入邮箱"})
    password = serializers.CharField(required=True, min_length=6, max_length=20)

    def validate(self, attrs):
        email = attrs.get('email')
        password = attrs.get('password')
        if email and password:
            user = OAUser.objects.get(email=email)
            # user = OAUser.objects.filter(email=email).first()
            if not user:
                raise serializers.ValidationError("请输入正确的邮箱！")
            if not user.check_password(password):
                raise serializers.ValidationError("账号或密码错误！")
            if user.status == UserStatusChoices.UNACTIVE:
                raise serializers.ValidationError("该用户未激活！")
            if user.status == UserStatusChoices.LOCKED:
                raise serializers.ValidationError("该用户已锁定，请联系管理员！")
            attrs['user'] = user
        else:
            raise serializers.ValidationError('请输入邮箱和密码！')
        return attrs


class DepartmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = OADepartment
        fields = '__all__'


class UserSerializer(serializers.ModelSerializer):
    department = DepartmentSerializer()

    class Meta:
        model = OAUser
        # fields = '__all__'
        exclude = ('password', 'groups', 'user_permissions')


class ResetPwdSerializer(serializers.Serializer):

    oldpwd = serializers.CharField(min_length=6, max_length=20)
    newpwd = serializers.CharField(min_length=6, max_length=20)
    pwd = serializers.CharField(min_length=6, max_length=20)

    def validate(self, attrs):
        oldpwd = attrs.get('oldpwd')
        newpwd = attrs.get('newpwd')
        pwd = attrs.get('pwd')

        user = self.context['request'].user
        if not user.check_password(oldpwd):
            raise exceptions.ValidationError("密码错误！")

        if pwd != newpwd:
            raise exceptions.ValidationError("密码不一致！")

        return attrs
